package com.test.jbpm.service.impl;


import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.test.jbpm.Pager;
import com.test.jbpm.dao.TroubleTicketDao;
import com.test.jbpm.domain.TroubleTicket;
import com.test.jbpm.service.TroubleTicketService;


@Service
public class TroubleTicketServiceImpl implements TroubleTicketService {

	private TroubleTicketDao troubleTicketDao;
	
	@Autowired
	public void setTroubleTicketDao(TroubleTicketDao troubleTicketDao) {
		this.troubleTicketDao = troubleTicketDao;
	}

	public void addOrUpdate(TroubleTicket troubleTicket) {
		if (troubleTicket.getId() == null) {
			troubleTicket.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
			troubleTicket.setStatus(TroubleTicket.NEW);
			troubleTicketDao.addOrUpdate(troubleTicket);
		}
	}

	public void del(Integer[] troubleTicketId) {
		for (Integer id : troubleTicketId) {
			troubleTicketDao.del(id);
		}
	}

	public Pager<TroubleTicket> findMyTroubleTicketList(Integer creatorId) {
		return troubleTicketDao.findMyTroubleTicketList(creatorId);
	}

	public TroubleTicket findTroubleTicketById(Integer troubleTicketId) {
		return troubleTicketDao.findTroubleTicketById(troubleTicketId);
	}

	public void dispatch(Integer[] troubleTicketIds) {
		for (Integer troublelTicketId : troubleTicketIds) {
			troubleTicketDao.dispatch(troublelTicketId);	
		}
	}

	public Pager<TroubleTicket> findApprovingTroubleTicketList(
			Integer approverId) {
		return troubleTicketDao.findApprovingTroubleTicketList(approverId);
	}

	public Pager<TroubleTicket> findApprovedTroubleTicketList(Integer approverId) {
		return troubleTicketDao.findApprovedTroubleTicketList(approverId);
	}

}
